package com.taptech.doufu.ui.view.topicview.adapter;

import android.content.Context;
import androidx.recyclerview.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.taptech.doufu.R;
import com.taptech.doufu.ui.activity.weex.StartWeexActivityUtils;
import com.taptech.doufu.app.WeMediaApplication;
import com.taptech.doufu.bean.DFHomeNovelBeans;
import com.taptech.doufu.constant.Constant;
import com.taptech.doufu.bean.EventBean;
import com.taptech.doufu.ui.view.topicview.UmengEventUtil;
import com.taptech.doufu.util.GlideRoundTransform;
import com.taptech.doufu.util.GlideUtil;

import java.util.List;

/**
 * Created by Administrator on 2017/4/13/013.
 */
public class BookListAdapter extends RecyclerView.Adapter<BookListAdapter.ViewHolder>{

    private List<DFHomeNovelBeans> list;
    private Context context;
    private String type;
    private String pageType;
    private EventBean eventBean;
    public BookListAdapter(Context context, List<DFHomeNovelBeans> list, String type,EventBean eventBean,String pageType){
        this.list = list;
        this.context = context;
        this.type = type;
        this.pageType = pageType;
        this.eventBean = eventBean;
    }

    public DFHomeNovelBeans getItem(int position){
        if (list != null && list.size()>position) {
            return list.get(position);
        }
        return null;
    }

    public void refrush(List<DFHomeNovelBeans> list) {
        if (list != null) {
            this.list = list;
            notifyDataSetChanged();
        }
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.topic_booklist_item, null);
        ViewHolder holder = new ViewHolder(view);
        holder.title = (TextView) view.findViewById(R.id.title);
        holder.tvStatusInfo = (TextView) view.findViewById(R.id.tvStatusInfo);
        holder.ivPic = (ImageView) view.findViewById(R.id.ivPic);
        holder.ivVip = (ImageView) view.findViewById(R.id.ivVip);
        holder.tvDesc = (TextView) view.findViewById(R.id.tvDesc);
        holder.tvAuthorDesc = (TextView) view.findViewById(R.id.tvAuthorDesc);
        holder.lay = view.findViewById(R.id.lay);
        holder.ivNum =  view.findViewById(R.id.ivNum);
        return holder;
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, int position) {
        final DFHomeNovelBeans model = getItem(position);
        if (model != null) {
            holder.title.setText(model.getTitle());
            if (TextUtils.isEmpty(model.getCover_left_top_icon_url())){
                if ("1".equals(model.getIs_vip())) {
                    holder.ivVip.setVisibility(View.VISIBLE);
                } else {
                    holder.ivVip.setVisibility(View.GONE);
                }
                holder.ivNum.setVisibility(View.GONE);
            }else{
                holder.ivNum.setVisibility(View.VISIBLE);
                holder.ivVip.setVisibility(View.GONE);
                GlideUtil.displayImage(context,holder.ivNum,model.getCover_left_top_icon_url());

            }
            if (model.getDesc() != null) {
                holder.tvDesc.setText(model.getDesc());
            }
            if (model.getUser() != null) {
                holder.tvAuthorDesc.setText(model.getUser().getNickname());
            }
            if (model.getStatus_info() != null) {
                holder.tvStatusInfo.setText(model.getStatus_info());
            }
            if (model.getCover() != null) {
                GlideUtil.displayRoundImage(holder.ivPic,model.getCover(),3,R.drawable.img_default_loading_l);
            }
            holder.lay.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    UmengEventUtil.addContentEvent(context,eventBean,type,pageType);
                    StartWeexActivityUtils.startBookListInfoActivity(context,String.valueOf(model.getId()));
                }
            });
            changeDayModelBg(holder);
        }
    }

    @Override
    public int getItemCount() {
        if (list != null) {
            return list.size();
        }
        return 0;
    }

    public class ViewHolder extends RecyclerView.ViewHolder{
        TextView title;
        ImageView ivPic;
        ImageView ivVip;
        TextView tvDesc;
        TextView tvAuthorDesc;
        ImageView ivNum;
        TextView tvStatusInfo;
        View lay;
        public ViewHolder(View itemView) {
            super(itemView);

        }
    }

    private void changeDayModelBg(ViewHolder viewHolder){
        if(WeMediaApplication.getInstance().isDayNightMode_Night){
            darkBg(viewHolder);
        }else {
            dayBg(viewHolder);
        }
    }

    private void dayBg(ViewHolder viewHolder) {
        viewHolder.lay.setBackgroundColor(context.getResources().getColor(R.color.white));
        viewHolder.title.setTextColor(context.getResources().getColor(R.color.text_color_22));
        viewHolder.tvDesc.setTextColor(context.getResources().getColor(R.color.text_color_7));
        viewHolder.tvStatusInfo.setTextColor(context.getResources().getColor(R.color.text_color_7));
        viewHolder.tvAuthorDesc.setTextColor(context.getResources().getColor(R.color.text_color_7));
        viewHolder.ivPic.setAlpha(1.0f);
        viewHolder.ivVip.setAlpha(1.0f);
        viewHolder.ivNum.setAlpha(1.0f);
    }

    private void darkBg(ViewHolder viewHolder) {
        viewHolder.lay.setBackgroundColor(context.getResources().getColor(R.color.fg_dark));
        viewHolder.title.setTextColor(context.getResources().getColor(R.color.title_text_dark));
        viewHolder.tvDesc.setTextColor(context.getResources().getColor(R.color.text_dark));
        viewHolder.tvStatusInfo.setTextColor(context.getResources().getColor(R.color.text_dark));
        viewHolder.tvAuthorDesc.setTextColor(context.getResources().getColor(R.color.text_dark));
        viewHolder.ivPic.setAlpha(Constant.imagViewAlpha);
        viewHolder.ivVip.setAlpha(Constant.imagViewAlpha);
        viewHolder.ivNum.setAlpha(Constant.imagViewAlpha);
    }
}
